/**
  * Created by lenovo on 2017/4/14.
  */
object Chapter2 {

  def signum(num: Int): Int = {
    if (num > 0) {
      1
    } else if (num < 0) {
      -1
    } else {
      0
    }
  }

  def countdown(n: Int): Unit = {
    for (i <- 0 to n reverse) {
      println(i)
    }
  }

  def product(s: String): Long = {
    var a: Long = 1L;
    s.foreach(a *= _.toLong)
    a
  }

  def product2(s: String): Long = {
    if (s.length == 1) return s.charAt(0).toLong
    else s.take(1).charAt(0).toLong * product2(s.drop(1))
  }


  def pf(x: Double, n: Int): Double = {
    if (n == 0) 1
    else if (n > 0 && n % 2 != 0) x * pf(x, n - 1)
    else if (n > 0 && n % 2 == 0) pf(x, n / 2) * pf(x, n / 2)
    else 1 / pf(x, -n)
  }


  def main(args: Array[String]): Unit = {
    println(signum(9))
    println(signum(-9))
    println(signum(0))

    for (i <- 0 to 10 reverse) {
      println(i);
    }

    countdown(3)

    var a: Long = 1L;
    for (i <- "kmtech") {
      a *= i.toLong;
    }
    println(a)

    a = 1;
    "kmtech".foreach(a *= _.toLong)
    println(a)

    println(product("kmtech"))
    println(product2("kmtech"))

    println(pf(3, 4))
    println(pf(3, 3))
    println(pf(3, -2))
  }


}
